package Controller.User;

import Dto.User;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "UpdateLogin", value = "/updateLogin")
public class UpdateLogin extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Integer id = Integer.valueOf(request.getParameter("id"));
        String newPassword = request.getParameter("newPassword");
        User user = new User();
        user.setPassword(newPassword);
        user.setId(id);
        HttpSession session =request.getSession();
        try {
            userService.updateLogin(user);
            Cookie cookie = new Cookie("nickname","");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            session.invalidate();
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }catch (SQLException e){
            System.out.println("sql异常");
            e.printStackTrace();
        }
    }
}
